package com.gk.panda.apis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.apis.entity.largeScreen.Enum.OrgEnum;
import com.gk.panda.apis.entity.largeScreen.vo.PersonVo;
import com.gk.panda.apis.mapper.OrgMapper;
import com.gk.panda.apis.service.OrgService;
import com.gk.panda.apis.service.UserService;
import com.gk.panda.pojo.Org;
import com.gk.panda.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {

    @Autowired
    private UserService userService;

    /**
     * 根据id删除机构
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(int id) {
        List<User> users = userService.searchUserByOrgId(id);
        QueryWrapper<Org> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Org> orgs = this.list(wrapper);
        if (users.size() > 0 || orgs.size() > 0) {
            return -1;//该机构下还有用户或子机构
        }
        return this.baseMapper.deleteById(id);
    }

    /**
     * 根据机构id获取自身及下属机构id列表
     * @param orgId
     * @return
     */
    @Override
    public List<Integer> getPuisneOrgIds(int orgId) {
        List<Integer> orgIds = new ArrayList<>();
        orgIds.add(orgId);
        List<Integer> puisneOrgIds = this.baseMapper.getPuisneOrgIds(orgId);
        orgIds.addAll(puisneOrgIds);
        return orgIds;
    }

    /**
     * 获取四个区域人员数量信息
     * @return
     */
    @Override
    public List<PersonVo> getPersonInfo() {
        List<PersonVo> vos = new ArrayList<>();
        OrgEnum[] orgEnums = OrgEnum.values();
        for (OrgEnum orgEnum : orgEnums) {
            PersonVo personVo = new PersonVo();
            List<Integer> orgIds = baseMapper.getPuisneOrgIds(orgEnum.getOrgId());//不包括自身
            orgIds.remove(orgEnum.getOrgId());
            Integer personNum = this.baseMapper.getPersonNumByOrgIds(orgIds);
            personVo.setAreaName(orgEnum.getName());
            personVo.setPersonNum(personNum);
            personVo.setBelayStationNum(orgIds.size());
            vos.add(personVo);
        }
        return vos;
    }

    /**
     * 根据机构id获取下属机构列表（不包括自身）
     * @param id
     * @return
     */
    @Override
    public List<Org> getPuisneOrgListById(Integer id) {
        if (id == null) id = OrgEnum.CHENGDU.getOrgId();
        List<Integer> orgIds = getPuisneOrgIds(id);
        OrgEnum[] orgEnums = OrgEnum.values();
        List<Integer> collect = Arrays.stream(orgEnums).map(OrgEnum::getOrgId).collect(Collectors.toList());
        orgIds.removeAll(collect);
        QueryWrapper<Org> wrapper = new QueryWrapper<>();
        wrapper.in("id",orgIds);
        return list(wrapper);
    }

    /**
     * 根据机构id获取总站机构id（分局及总站返回自身id）
     * @param totalOrgId
     * @return
     */
    @Override
    public Integer getTotalOrgId(Integer totalOrgId) {
        QueryWrapper<Org> wrapper = new QueryWrapper<>();
        wrapper.eq("id",totalOrgId);
        Org org = this.getOne(wrapper);
        if (org.getParentId() == 0 || org.getParentId() == 1) return org.getId();
        return org.getParentId();
    }

    /**
     * 根据机构id获取机构缩写
     * @param id
     * @return
     */
    @Override
    public String getAbbrById(Integer id) {
        return getById(id).getAbbr();
    }

    /**
     * 分页查询机构列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Org> getOrgPage(int pageNumber, int pageSize) {
        List<Org> orgs = baseMapper.getOrgList();
//        //当前页
//        int current = dto.getPageNumber();
//        //每页数据条数
//        int size = dto.getPageSize();
//        Page<BiologyFileVo> page = new Page<>(current,size);
//        List<Org> pageList = new ArrayList<>();
        //计算当前页第一条数据的下标
//        int currId = pageNumber>1 ? (pageNumber-1)*pageSize:0;
//        for (int i=0; i<pageSize && i<count - currId;i++){
//            pageList.add(orgs.get(currId+i));
//        }
        Page<Org> page = new Page<>();
        int count = orgs.size();
        page.setSize(pageSize);
        page.setCurrent(pageNumber);
        page.setTotal(count);
        //计算分页总页数
        page.setPages(count %10 == 0 ? count/10 :count/10+1);
        page.setRecords(orgs);
        return page;
    }

    /**
     * 根据机构id分页查询自身及下属机构列表
     * @param pageNumber
     * @param pageSize
     * @param orgId
     * @return
     */
    @Override
    public Page<Org> getOrg(int pageNumber, int pageSize, Integer orgId) {
        Page<Org> page = new Page<>(pageNumber, pageSize);
        if (orgId != null) {
            List<Integer> orgIds = getPuisneOrgIds(orgId);
            QueryWrapper<Org> wrapper = new QueryWrapper<>();
            wrapper.in("id",orgIds);
            return page(page,wrapper);
        }
        return page(page);
    }
}
